import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import requireTransform from "vite-plugin-require-transform";
import optimizer from 'vite-plugin-optimizer'

import path from 'path'
// https://vite.dev/config/
export default defineConfig((command, mode) => {

  // 获取各种环境下对应的变量
  let env = loadEnv(mode, process.cwd())

    return {
      plugins: [vue(), requireTransform({fileRegex: /.js$|.vue$/}), optimizer({
        electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`
      })],
      server: {
        port: 8888,
        cors: true, // 允许跨域
        hmr: true, // 开启热更新
        // proxy: {
        //   [env.VITE_APP_BASE_API]: {
        //     target: env.VITE_SERVE,
        //     changeOrigin: true,
        //     rewrite: (path) => path.replace(/^\/aiApi/, ''),
        //   },
        // },
        proxy: {
          '/aiApi': {  // 直接写死路径，避免依赖环境变量
            target: "https://maas-api.cn-huabei-1.xf-yun.com",
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/aiApi/, ''),  // 移除 /aiApi 前缀
            secure: false,  // 如果是 HTTPS，可能需要关闭证书验证（开发环境用）
          }
        }
      },
      resolve: {
        alias: {
          '@': path.resolve(__dirname, './src')
        }
      },
      base: './',
    }
  }
)

